package com.db;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * 
 * 连接管理器
 * 
 * @author emmerich@126.com
 * @createDate 2014年7月28日
 *
 */
public class ConnectionManager {

    private static ConnectionManager instance;
    private static ComboPooledDataSource dataSource;

    private ConnectionManager() {
        try {
            dataSource = new ComboPooledDataSource();
            dataSource.setUser("ka");
            dataSource.setPassword("ucweb@2012");
            dataSource
                    .setJdbcUrl("jdbc:mysql://ka.test5.9game.cn:9222/test?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8");
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
            dataSource.setInitialPoolSize(2);
            dataSource.setMinPoolSize(10);
            dataSource.setMaxPoolSize(50);
            dataSource.setMaxStatements(50);
            dataSource.setMaxIdleTime(60);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }

    public Connection getConn() throws SQLException{
        return dataSource.getConnection();
    }
    
    public static synchronized ConnectionManager instance() {
        
        if(instance==null){
            instance = new ConnectionManager();
        }
        
        return instance;
    }
    
}
